<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <div th:insert="commons.html"></div>
  <SCRIPT type="text/javascript">

    var setting = {
      data: {
        simpleData: {
          enable: true
        }
      },
      check: {
        enable: true,
        autoCheckTrigger: true
      },
      async: {
        enable: true,
        url: "/menu/getInfoTree",
      },
      view : {
        addHoverDom: function(treeId, treeNode){
          var aObj = $("#" + treeNode.tId + "_a");
          if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
          var s = '<span id="btnGroup'+treeNode.tId+'">';
          if ( treeNode.level == 0 ) {
            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
          } else if ( treeNode.level == 1 ) {
            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
            if (treeNode.children == undefined) {
              s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#" >删除</a>';
            }
            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
          } else if ( treeNode.level == 2 ) {
            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
            s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#">删除</a>';
          }
          s += '</span>';
          aObj.after(s);
        },
        removeHoverDom: function(treeId, treeNode){
          $("#btnGroup"+treeNode.tId).remove();
        }
      }
    };

    $(document).ready(function(){
      $.fn.zTree.init($("#treeDemo"), setting);
    });
    function addNode(pid){
      BootstrapDialog.show({
        title: '增加',
        message: $('<div></div>').load('/menu/toAdd'),
        closeable: true,
        buttons: [{
          label: '确定',
          action: function (dialog) {
            $.ajax({
              url : "/menu/increase",
              type : "post",
              dataType : "json",
              data : $("#menu_add").serialize() + "&pid=" + pid,
              success : function(data){
                if(data){
                  toastr.success("增加成功")
                  dialog.close();
                  $.fn.zTree.init($("#treeDemo"), setting);
                }
              },
              error : function(data){
                console.log(data)
                toastr.error("增加操作执行失败")
              }
            })
          }
        }]
      });
    }
    function deleteNode(id){
      if(confirm("真的要狠心吗")){
        $.ajax({
          url:"/menu/deleteById",
          data:{id:id},
          type:"get",
          dataType:"json",
          async:true,
          success:function (result){
            toastr.success("删除成功");
            $.fn.zTree.init($("#treeDemo"), setting);
          },
          error:function (result){
            console.log(id);
            toastr.error("执行操作失败");
          }
        })
      }
    }
    function editNode(id){
      BootstrapDialog.show({
        title: '修改',
        message: $('<div></div>').load('/menu/toUpdate/'+id),
        closeable: true,
        buttons: [{
          label: '确定',
          action: function (dialog) {
            $.ajax({
              url : "/menu/updateInfo",
              type : "post",
              dataType : "json",
              data : $("#menu_update").serialize(),
              success : function(data){
                toastr.success("更新成功")
                dialog.close();
                $.fn.zTree.init($("#treeDemo"), setting);
              },
              error : function(data){
                console.log(data)
                toastr.error("更新操作执行失败")
              }
            })
          }
        }]
      });
    }
  </SCRIPT>
</head>
<body>
<div class="zTreeDemoBackground left">
  <ul id="treeDemo" class="ztree"></ul>
</div>
</body>
</html>
